import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:flutter_inappwebview/flutter_inappwebview.dart';
import 'package:get/get.dart';
import 'package:get/get_rx/src/rx_types/rx_types.dart';
import 'package:get/get_state_manager/src/simple/get_controllers.dart';
import 'package:mziot/app/data/circle_detail_entity.dart';
import 'package:mziot/app/data/comment_list_entity.dart';
import 'package:mziot/app/modules/circle/circle_provider.dart';
import 'package:mziot/app/tools/tools.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';

class CircleDetailController extends GetxController {
  CircleProvider provider = CircleProvider();
  TextEditingController editingController = TextEditingController();
  RefreshController controller = RefreshController();
  InAppWebViewController? webViewController;
  final height = 1.obs;
  final detailEntity = CircleDetailEntity().obs;
  String? title;
  int? cid;
  int page = 0;
  final comments = <CommentListList>[].obs;
  bool hasNext = true;

  @override
  void onInit() {
    super.onInit();

    title = Get.parameters['title'];
    cid = int.parse(Get.parameters['id'] as String);

    fetchDetail();
    fetchCommets();
    height.listen((p0) {
      if (height >= 1) {
        EasyLoading.dismiss();
      }
    });
  }

  @override
  void onReady() {
    super.onReady();
  }

  void loadData() {
    if (!hasNext) {
      controller.loadNoData();
    }
    page += 1;
    fetchCommets();
  }

  void fetchDetail() {
    EasyLoading.show();
    provider.takeCircleDetail(cid ?? 0).then((value) {
      if (value.isSuccess) {
        detailEntity.value = value.data!;
      } else {
        EasyLoading.dismiss();
      }
    }).catchError((_){
      EasyLoading.dismiss();
    });
  }

  void fetchAgree() {
    EasyLoading.show();
    provider.takeCircleAgree(cid ?? 0).then((value) {
      if (value.isSuccess) {}
      showToast(value.msg ?? '');
    }).whenComplete(() {
      EasyLoading.dismiss();
    });
  }

  void fetchCommets() {
    if (detailEntity.value.id == null) {
      EasyLoading.show();
    }
    provider.takeCircleCommentList(page, cid ?? 0).then((value) {
      if (value.isSuccess) {
        final models = value.data?.list ?? [];
        if (page == 0) {
          comments.value = models;
          controller.refreshCompleted();
        } else {
          comments.value.addAll(models);
        }
        hasNext = value.data?.hasNext ?? false;
        if (hasNext) {
          controller.loadComplete();
        } else {
          controller.loadNoData();
        }
      }
    }).catchError((_) {
      controller.refreshCompleted();
      controller.loadComplete();
      EasyLoading.dismiss();
    });
  }

  void fetchSendText() {
    if (editingController.text.isEmpty) {
      return;
    }
    EasyLoading.show();
    final text = editingController.text;
    provider.takeCircleComment(text, cid ?? 0).then((value) {
      if (value.isSuccess) {
        fetchCommets();
        editingController.text = '';
        FocusScope.of(Get.context!).requestFocus(FocusNode());
      }
      showToast(value.msg ?? '');
    }).whenComplete(() {
      EasyLoading.dismiss();
    });
  }

  @override
  void onClose() {
    EasyLoading.dismiss();
    editingController.dispose();
    controller.dispose();
  }
}
